package com.weixing.mall.provider.controller.admin;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.dto.SupplierDto;
import com.weixing.mall.provider.model.vo.SupplierVo;
import com.weixing.mall.provider.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wx
 * @Date 2019/8/6 13:57
 * @Version 1.0
 */
@RestController
@RequestMapping("supplier")
@Api(value = "供应商api",tags = "供应商")
public class SupplierController extends BaseController {

    @Autowired
    private ISupplierService supplierService;

    @ApiOperation(value = "查询供应商")
    @GetMapping("")
    public Result list(String supplierName){
        return ResultUtil.success(supplierService.selectOfPage(initMpPage(),supplierName));
    }
    @ApiOperation(value = "查询供应商")
    @GetMapping("/list")
    public Result listByAble(){
        return ResultUtil.success(supplierService.selectAll(WhetherEnum.NO.getValue()));
    }
    @ApiOperation(value = "新增")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@Validated SupplierDto supplierDto){
   /*     try{
            Supplier entity=  supplierService.getOne(new QueryWrapper<Supplier>().eq("supplier_name",supplierDto.getSupplierName()).or().eq("supplier_sn",supplierDto.getSupplierSn()));
            if(supplierDto.getId()==null&&entity!=null){

                return ResultUtil.error("供应商名称或者编号重复，请重新填写");
            }
        }catch (Exception e){
            return ResultUtil.error("发现未知错误，请联系管理员");
        }*/
        supplierService.save(supplierDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "批量删除供应商")
    @PostMapping("delAll/{ids}")
    public Result delAll(@PathVariable int [] ids){
        supplierService.deleteAll(ids);
        return ResultUtil.success();
    }

    @ApiOperation(value = "查询所有供应商")
    @GetMapping("selectAll")
    public Result selectAll(){
        List<SupplierVo> suppliers = supplierService.selectAll(WhetherEnum.YES.getValue());
        return ResultUtil.success(suppliers);
    }
}
